package com.richman.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import com.richman.ps38.entity.Event;
import com.richman.ps38.entity.ISNPS38Event;
import com.richman.view.HGPS38EventPane.MyTableModel;

public class HGPS38EventPane extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -88L;
	
	private  Vector<String[]> detailsData = new Vector<String[]>();
	
	//public Vector<Event> gameDetailsVec = new Vector<Event>(); 
	

	
	
	
	
	
	
	MyTableModel tableMode = new MyTableModel();

	
	JTable table = null;

	/**
	 * Create the panel.
	 */
	public HGPS38EventPane() {
		setLayout(new BorderLayout(0, 0));

		
	    table = new JTable(tableMode);
	    
	    table.getColumnModel().getColumn(0).setPreferredWidth(120);
	    
	    table.setRowHeight(30);
	    
	    table.setFont(new java.awt.Font("黑体", Font.PLAIN, 15));
		
		
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);
		


	}
	
	
	
	public void updateHGPS38EventTable(Vector<ISNPS38Event> gamedetails){
		
		try{
			
			if(detailsData.size() != 0){
				detailsData.clear();
			}
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			for(int i = 0; i < gamedetails.size(); i++) {
				
				ISNPS38Event game = gamedetails.elementAt(i);
				
				String time = game.duringTime;
				if(time.equals("")) {
					time = game.eventTime;
				}
				
				//{ "序号", "联赛", "时间", "球队", "全场让球盘", "主队赔率", "客队赔率", "大小盘", "大球","小球", "半场让球盘", "主队赔率", "客队赔率", "大小盘", "大球", "小球", "比分"};
				String[] item = {Integer.toString(i+1), game.leagueName,time, game.homeTeam + "-vs-" + game.awayTeam,
									game.wholeRQPPankou, game.wholeRQPPankouUp, game.wholeRQPPankouDown,
									game.wholeDXPPankou, game.wholeDXPPankouUp, game.wholeDXPPankouDown,
									game.halfRQPPankou, game.halfRQPPankouUp, game.halfRQPPankouDown,
									game.halfDXPPankou, game.halfDXPPankouUp, game.halfDXPPankouDown,
									game.homeTeamScore + ":" + game.awayTeamScore};
				
				detailsData.add(item);
				
			}
			
			
			
			
			
			
			
			
			setOneRowBackgroundColor();
			
			tableMode.updateTable();

			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	
	
    public void setOneRowBackgroundColor() {
    	

    	
        try {  
        	
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
            	
            	
  
                public Component getTableCellRendererComponent(JTable table,  
                        Object value, boolean isSelected, boolean hasFocus,  
                        int row, int column) {  

                	
                    if (row%2==1) {  
                    	setBackground(new Color(246,246,246));  
                        
                    }else{  
                        setBackground(new Color(222,222,243));  
                    	//setBackground(new Color(1,1,1));  
                      
                    }  
                    
                    setHorizontalAlignment(JLabel.CENTER);
  
                    return super.getTableCellRendererComponent(table, value,  
                            isSelected, hasFocus, row, column);  
                }  
            };  
            int columnCount = table.getColumnCount();  
            for (int i = 0; i < columnCount; i++) {  
            	table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
            }  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    
    }  
	
	
	
	

    public class MyTableModel extends AbstractTableModel  
    {  
        /**
		 * 
		 */
		private static final long serialVersionUID = 10086L;
		/* 
         * 这里和刚才一样，定义列名和每个数据的值 
         */  
        String[] columnNames =  
        { "序号", "联赛", "时间", "球队", "全场让球盘", "合并赔率1", "合并赔率2", "大小盘", "合并赔率1 ","合并赔率2 ", "半场让球盘 ", "合并赔率1 ", "合并赔率2 ", "大小盘 ", "合并赔率1 ", "合并赔率2 "};
        

  
        /** 
         * 构造方法，初始化二维数组data对应的数据 
         */  
        public MyTableModel()  
        {  

        }  
  
        // 以下为继承自AbstractTableModle的方法，可以自定义  
        /** 
         * 得到列名 
         */  
        @Override  
        public String getColumnName(int column)  
        {  
            return columnNames[column];  
        }  
          
        /** 
         * 重写方法，得到表格列数 
         */  
        @Override  
        public int getColumnCount()  
        {  
            return columnNames.length;  
        }  
  
        /** 
         * 得到表格行数 
         */  
        @Override  
        public int getRowCount()  
        {         
	        if(null == detailsData){
	    		return 0;
	    	}
            return detailsData.size();  
        }  
  
        /** 
         * 得到数据所对应对象 
         */  
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex)  
        {  
            //return data[rowIndex][columnIndex];
        	return detailsData.elementAt(rowIndex)[columnIndex];
        }  
  
        /** 
         * 得到指定列的数据类型 
         */  
        @Override  
        public Class<?> getColumnClass(int columnIndex)  
        {  
        	
        	return String.class;
        	
//        	if(detailsData.size() == 0){
//        		return String.class;
//        	}
//
//            return detailsData.elementAt(0)[columnIndex].getClass();
        	
        }  
  
        /** 
         * 指定设置数据单元是否可编辑.这里设置"姓名","学号"不可编辑 
         */  
        @Override  
        public boolean isCellEditable(int rowIndex, int columnIndex)  
        {  
            return false;
        }  
          
        /** 
         * 如果数据单元为可编辑，则将编辑后的值替换原来的值 
         */  
        @Override  
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)  
        {  
            detailsData.elementAt(rowIndex)[columnIndex] = (String)aValue;  
            /*通知监听器数据单元数据已经改变*/  
            fireTableCellUpdated(rowIndex, columnIndex);  
        }  
        
        public void updateTable(){
        	fireTableDataChanged();
        }
        
  
    }  
}
